home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / net / socket~4.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  4.1 KB  |  146 lines

  1. /*
  2.  * @(#)SocketInputStream.java    1.11 95/11/06 Jonathan Payne
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.net;
  21.  
  22. import java.io.IOException;
  23. import java.io.FileInputStream;
  24.  
  25. /**
  26.  * This stream extends FileInputStream to implement a
  27.  * SocketInputStream. Note that this class should <b>NOT</b> be
  28.  * public.
  29.  *
  30.  * @version     1.11, 11/06/95
  31.  * @author    Jonathan Payne
  32.  * @author    Arthur van Hoff
  33.  */
  34. class SocketInputStream extends FileInputStream
  35. {
  36.     private boolean eof;
  37.     private SocketImpl impl;
  38.     private byte temp[] = new byte[1];
  39.  
  40.     /**
  41.      * Creates a new SocketInputStream. Can only be called
  42.      * by a Socket. This method needs to hang on to the owner Socket so
  43.      * that the fd will not be closed.
  44.      * @param impl the implemented socket input stream
  45.      */
  46.     SocketInputStream(SocketImpl impl) throws IOException {
  47.     super(impl.getFileDescriptor());
  48.     this.impl = impl;
  49.     }
  50.  
  51.     /** 
  52.      * Reads into an array of bytes at the specified offset using
  53.      * the received socket primitive. 
  54.      * @param b the buffer into which the data is read
  55.      * @param off the start offset of the data
  56.      * @param len the maximum number of bytes read
  57.      * @return the actual number of bytes read, -1 is
  58.      *          returned when the end of the stream is reached. 
  59.      * @exception IOException If an I/O error has occurred.
  60.      */
  61.     private native int socketRead(byte b[], int off, int len)
  62.     throws IOException;
  63.  
  64.     /** 
  65.      * Reads into a byte array data from the socket. 
  66.      * @param b the buffer into which the data is read
  67.      * @return the actual number of bytes read, -1 is
  68.      *          returned when the end of the stream is reached. 
  69.      * @exception IOException If an I/O error has occurred. 
  70.      */
  71.     public int read(byte b[]) throws IOException {
  72.     return read(b, 0, b.length);
  73.     }
  74.  
  75.     /** 
  76.      * Reads into a byte array <i>b</i> at offset <i>off</i>, 
  77.      * <i>length</i> bytes of data.
  78.      * @param b the buffer into which the data is read
  79.      * @param off the start offset of the data
  80.      * @param len the maximum number of bytes read
  81.      * @return the actual number of bytes read, -1 is
  82.      *          returned when the end of the stream is reached. 
  83.      * @exception IOException If an I/O error has occurred.
  84.      */
  85.     public int read(byte b[], int off, int length) throws IOException {
  86.     if (eof) {
  87.         return -1;
  88.     }
  89.     int n = socketRead(b, off, length);
  90.     if (n <= 0) {
  91.         eof = true;
  92.         return -1;
  93.     }
  94.     return n;
  95.     }
  96.  
  97.     /** 
  98.      * Reads a single byte from the socket. 
  99.      */
  100.     public int read() throws IOException {
  101.     if (eof) {
  102.         return -1;
  103.     }
  104.  
  105.      int n = read(temp, 0, 1);
  106.     if (n <= 0) {
  107.         return -1;
  108.     }
  109.     return temp[0] & 0xff;
  110.     }
  111.  
  112.     /** 
  113.      * Skips n bytes of input.
  114.      * @param n the number of bytes to skip
  115.      */
  116.     public int skip(int numbytes) throws IOException {
  117.     int n = numbytes;
  118.     byte data[] = new byte[n];
  119.     while (n > 0) {
  120.         n -= read(data, 0, n);
  121.     }
  122.     return numbytes;
  123.     }
  124.  
  125.     /**
  126.      * Returns the number of bytes that can be read without blocking.
  127.      * @return the number of immediately available bytes
  128.      */
  129.     public int available() throws IOException {
  130.     return impl.available();
  131.     }
  132.  
  133.     /**
  134.      * Closes the stream.
  135.      */
  136.     public void close() throws IOException {
  137.     impl.close();
  138.     }
  139.  
  140.     /** 
  141.      * Overrides finalize, the fd is closed by the Socket.
  142.      */
  143.     protected void finalize() {}
  144. }
  145.  
  146.